package org.xbzheng.restful_study.service;

import org.xbzheng.restful_study.tool.ExceptionTool;
import org.xbzheng.restful_study.util.ResponseEntity;
import org.xbzheng.restful_study.util.ResponseError;
import org.xbzheng.restful_study.util.ResponseStatus;

/**
 * @Created by xiaobing.zheng on 2016/12/27.
 */
public class BaseService {
    ResponseEntity getResponseEntity(final Exception e) {
        return getResponseEntity(null, null, e);
    }
    ResponseEntity getResponseEntity(final ResponseStatus status) {
        return getResponseEntity(status, null, null);
    }

    ResponseEntity getResponseEntity(final Object[] results) {
        return getResponseEntity(ResponseStatus.OK, results, null);
    }

    ResponseEntity getResponseEntity(final ResponseStatus status, final Object[] results) {
        return getResponseEntity(status, results, null);
    }

    ResponseEntity getResponseEntity(final ResponseStatus status, final Exception e) {
        return getResponseEntity(status, null, e);
    }

    ResponseEntity getResponseEntity(final ResponseStatus status, final Object[] results, final Exception e) {
        final ResponseEntity entity = new ResponseEntity();
        if(status != null){
            entity.setStatus(status);
        }
        if (results != null) {
            entity.setData(results);
        } else if (e != null) {
            entity.setStatus(ResponseStatus.ERROR);
            final ResponseError error = new ResponseError(500, ExceptionTool.convertExceptionToString(e));
            entity.setError(error);
        }
        return entity;
    }
}
